package com.huaxin.webexam.web.fore;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.huaxin.webexam.bean.Admin;
import com.huaxin.webexam.bean.Student;
import com.huaxin.webexam.bean.Teacher;
import com.huaxin.webexam.service.AdminService;
import com.huaxin.webexam.service.impl.AdminServiceImpl;
import com.huaxin.webexam.web.common.CommonServlet;

public class AdminServlet extends CommonServlet {

	public String adminLoginUI(HttpServletRequest request, HttpServletResponse response) {
		return "fore/login";
	}

	public String adminLogin(HttpServletRequest request, HttpServletResponse response) {
		String aname = request.getParameter("aname");
		String apassword = request.getParameter("apassword");
		System.out.println("aname=" + aname);
		System.out.println("apassword=" + apassword);

		Admin admin = new Admin();
		admin.setAname(aname);
		admin.setApassword(apassword);

		AdminService adminService = new AdminServiceImpl();
		List<Admin> login = adminService.login(admin);
		System.out.println("~~~~~~~~login=" + login);
		if (login.size() == 0) {
			request.setAttribute("msg", "密码或者账号错误！！！");
			return "fore/login";
		} else {
			return showStudentList(request, response);
		}
	}

	public String showStudentList(HttpServletRequest request, HttpServletResponse response) {

		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~");// 数据中查找所有学生的列表
		AdminService adminService = new AdminServiceImpl();
		List<Student> studentList = adminService.StudentList();
		// 放到session域中 students
		HttpSession session = request.getSession();
		session.setAttribute("students", studentList);
		return "fore/index";
	}

	public String deleteStudent(HttpServletRequest request, HttpServletResponse response) {
		// 拿学生sno
		String sno = request.getParameter("sno");
		System.out.println("sno=" + sno);
		Student student = new Student();
		student.setSno(sno);
		// 删除
		AdminService adminService = new AdminServiceImpl();
		adminService.deleteStudent(student);
		// 放到request域中 smsg 回显删除成功
		// request.setAttribute("smsg", "删除成功");
		return showStudentList(request, response);
	}

	public String showTeacherList(HttpServletRequest request, HttpServletResponse response) {
		// 数据中查找所有学生的列表
		AdminService adminService = new AdminServiceImpl();
		List<Teacher> teacherList = adminService.TeacherList();
		// 放到session域中 teachers
		HttpSession session = request.getSession();
		session.setAttribute("teachers", teacherList);
		return "fore/teacher";
	}

	public String deleteTeacher(HttpServletRequest request, HttpServletResponse response) {
		// 拿教师tno
		String tno = request.getParameter("tno");
		Teacher teacher = new Teacher();
		teacher.setTno(tno);
		// 删除
		AdminService adminService = new AdminServiceImpl();
		adminService.deleteTeacher(teacher);
		return showTeacherList(request, response);
	}
}
